//
//  TodaySpo2ProgressView.swift
//  MoyoungHome
//
//  Created by 易先生 on 2022/7/25.
//

import UIKit
import MYModule

class TodayBloodOxygenProgressView: View {
    
    var o2: Int = 0 {
        didSet {
            
            let progress = getProgress(spo2: o2)
            configProgress(progress: progress)
        }
        
    }

	override func makeUI() {
		super.makeUI()
        
    
		self.addSubview(bgView)
		bgView.addSubview(progressView)

		bgView.snp.makeConstraints { make in
			make.edges.equalToSuperview()
		}
		progressView.snp.makeConstraints { make in
			make.leading.top.bottom.equalToSuperview()
			make.width.equalTo(0)
		}
	}
    
    private func getProgress(spo2: Int) -> CGFloat {
        
        var progress: CGFloat = 0.0
        
        var precent = 0
        if spo2 >= 80 {
            precent = spo2 - 80
        }
        progress = CGFloat(precent)/20.0
        return progress
    }

	/// 血氧进度设置
	/// - Parameter width: 宽度
	public func configProgress(progress: CGFloat) {
        
		progressView.snp.remakeConstraints { make in
			make.top.bottom.equalToSuperview()
			make.leading.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(progress)
		}
	}
    


	public func configDetailBloodProgress(height: CGFloat, top: CGFloat) {

		progressView.snp.remakeConstraints { make in
			make.top.equalToSuperview().offset(top)
			make.leading.trailing.equalToSuperview()
			make.height.equalTo(height)
		}
	}

	public func configColor(bgColor: UIColor, progressColor: UIColor) {
		progressView.backgroundColor = progressColor
		bgView.backgroundColor = bgColor
		bgView.layer.borderColor = progressColor.cgColor
	}

	private lazy var bgView: View = {
		let bgView = View()
        bgView.layer.cornerRadius = 12.0
        bgView.layer.masksToBounds = true
		return bgView
	}()

	private lazy var progressView: UIView = {
		let progressView = UIView()
        progressView.layer.cornerRadius = 12.0
		return progressView
	}()

}
